#include "AppInitMessage.h"
#include "qwidget.h"
#include <QVariant>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>
AppInitMessage::AppInitMessage(QObject *parent) : QObject(parent)
{

}

bool AppInitMessage::eventFilter(QObject * obj,QEvent * evt)
{
    QWidget * wget = (QWidget *) obj;

    if (!wget->property("canMove").toBool()) {
        return QObject::eventFilter(obj, evt);
    }

    static QPoint mousePoint;
    static bool mousePress = false ;

    QMouseEvent * event = static_cast<QMouseEvent*>(evt);
    if(event->type()==QEvent::MouseButtonPress)
    {
        if(event->button()==Qt::LeftButton)
        {
            mousePress = true;
            //求出原始坐标，窗体在左上角(原点位置 )，相对于屏幕的位置
            mousePoint = event->globalPos() - wget->pos();
            qDebug() <<"screen pos "<<event->globalPos()<<"client pos"<< mousePoint;
            return true;
        }
    }else if(event->type() == QEvent::MouseButtonRelease)
    {
        mousePress = false;
        return true;
    }else if(event->type() == QEvent::MouseMove)
    {
        if (mousePress && (event->buttons() && Qt::LeftButton))
        {
            //左上原点位置
            wget->move(event->globalPos()-mousePoint);
            qDebug() <<"screen pos "<<event->globalPos()<<"client pos"<< mousePoint;
            return true;
        }
    }
    return QObject::eventFilter(obj,evt);
}
void AppInitMessage::startMessageEvent()
{
    qApp->installEventFilter(this);
}
